package com.liming.common.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import java.util.Collection;

/**
 * @author liming
 * @date 2025/1/8
 */
@Slf4j
public class AntPathMatchUtils {

    /**
     * 判断路径是否匹配
     */
    private static final AntPathMatcher antPathMatcher = new AntPathMatcher();

    /**
     * @param pattern     匹配模式
     * @param requestPath 请求路径
     * @return 是否匹配
     */
    public static boolean match(String pattern, String requestPath) {
        return antPathMatcher.match(pattern, requestPath);
    }

    /**
     * @param patterns    能够访问的路径集合
     * @param requestPath 请求路径
     * @return 是否能够访问
     */
    public static boolean permitPath(Collection<String> patterns, String requestPath) {
        for (String pattern : patterns) {
            if (match(pattern, requestPath)) {
                return true;
            }
        }
        return false;
    }
}
